﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="MicroTwit.cs" company="Stig Atle Steffensen">
// Copyright (c) .  All rights reserved.
// </copyright>
// <summary>MicroTwit is a free application to use with twitter.com's service.</summary>
//-------------------------------------------------------------------------------------------------
namespace MicroTwit
{
    using System;
    using System.Windows.Forms;
    using Twitterizer.Framework;

    /// <summary>
    /// app to use with twitter.
    /// </summary>
    public partial class MicroTwit : Form
    {
        public MicroTwit()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Settings.Default.UserName == string.Empty)
            {
                MessageBox.Show("Please set username and password.");
                return;
            }

            var twitter = new Twitter(Settings.Default.UserName, Settings.Default.Password);
            twitter.Status.Update(textBox1.Text);
            label1.Text = string.Format("Last Update: [{0} - {1}]", 
                DateTime.Now.ToShortTimeString(),
                DateTime.Today.ToShortDateString());
            
            textBox1.Text = string.Empty;
            Settings.Default.lastupdate = label1.Text;
            Settings.Default.Save();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
            label1.Text = Settings.Default.lastupdate;
            if (Settings.Default.UserName == string.Empty)
                MessageBox.Show("Please set username and password.");
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var twitITSettings = new TwitITSettings();
            twitITSettings.Show();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char) Keys.Enter:
                    var twitter = new Twitter(Settings.Default.UserName, Settings.Default.Password);
                    twitter.Status.Update(textBox1.Text);
                    textBox1.Text = string.Empty;
                    label1.Text = string.Format("Last Update: [{0} - {1}]", 
                        DateTime.Now.ToShortTimeString(),
                        DateTime.Today.ToShortDateString());
                    Settings.Default.lastupdate = label1.Text;
                    Settings.Default.Save();
                    break;
            }
        }

        private void trayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var tray = new NotifyIcon())
            {
                Hide();
                ShowInTaskbar = false;
                tray.Visible = true;
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            Show();
            ShowInTaskbar = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            NotifyIcon tray = new NotifyIcon();
            tray.Visible = false;
        }
    }

}